//
//  QPJHTool.m
//  QPJHLightSDK
//
//  Created by qply on 2021/4/26.
//  Copyright © 2021 Vanney. All rights reserved.
//

#import "QPJHTool.h"
#import "SQPJHNetworkManager.h"
#import "QPJHFunRoleModel.h"
#import "QPJHFunDataFormat.h"
#import "QPConstHeader.h"
#import "QPJHUrlHeader.h"
#import "QPJHCountdown.h"

#define WeakSelf(ws) __weak __typeof(&*self)weakSelf = self

@interface QPJHTool ()
@property(nonatomic, strong) QPJHFunRoleModel *roleinfo;

@end

@implementation QPJHTool
+ (QPJHTool *)sharedInstance {
    static QPJHTool *sharedManagerInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManagerInstance = [[self alloc] init];
        
    });
    return sharedManagerInstance;
}

#pragma mark - 初始化接口
- (void)bootIndexWithRoleInfo:(QPJHFunRoleModel *)roleinfo isStartTimer:(BOOL)isStart isReport:(BOOL)isReport {
    
    self.roleinfo = roleinfo;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"super_user_id"] = roleinfo.super_user_id;
    params[@"game_id"] = [QPJHFunDataFormat QPJHGetGameID];
    params[@"role_id"] = roleinfo.super_role_id;
    params[@"server_id"] = roleinfo.service_id;
    params[@"player_id"] = roleinfo.role_id;
//    __weak typeof(self) weakSelf = self;
    WeakSelf(ws);
    [SQPJHNetworkManager getWithURL:AppendUrl(@"/report/boot") Params:params success:^(id responseObject) {
//        NSLog(@"/boot/index==%@--%@",params,responseObject);
//        __strong __typeof(weakSelf) strongSelf = weakSelf;
        NSString *code = [NSString stringWithFormat:@"%@", responseObject[@"code"]];
        if ([code isEqualToString:SuccessCode]) {
//            NSDictionary *data = [responseObject bgl_safeDictionaryForKeyPath:@"data"];
            
            if (isReport == YES) {
                if (isStart == NO) {
                    [self startAllTimerWithRoleMode:roleinfo];

                }
                
            }
            
        }
    } failure:^(NSString *error) {
        
    }];
}
- (void)startAllTimerWithRoleMode:(QPJHFunRoleModel *)roleinfo {
    //case 2 角色上报在线时长 秒 ()
    [self repeatOnlineTimeWithTime:60 RoleModel:roleinfo];

}
- (void)repeatOnlineTimeWithTime:(NSInteger)time RoleModel:(QPJHFunRoleModel *)roleinfo {
//    __weak typeof(self) weakSelf = self;
    WeakSelf(ws);
    [[QPJHCountdown shareInstance] time:time timeDown:^(NSTimeInterval time) {
        
    } finish:^(id  _Nonnull timerName) {
        
        __strong __typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf bootOnlineWith:roleinfo];
        [strongSelf repeatOnlineTimeWithTime:time RoleModel:roleinfo];
    } timeKey:@"online_time_report"];
}

#pragma mark - 时长上报
- (void)bootOnlineWith:(QPJHFunRoleModel *)roleInfo {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSString *random = [QPJHFunDataFormat QPJHMD5GetRandomStr];
    NSString *md5str = [NSString stringWithFormat:@"%@%@%@%@%@%@",[QPJHFunDataFormat QPJHGetGameID],roleInfo.super_role_id,roleInfo.service_id,roleInfo.super_user_id,random,QPJHPropsKey];
    NSString *sign = [QPJHFunDataFormat QPJHMD5WithNSString:md5str];
    
    params[@"super_user_id"] = roleInfo.super_user_id;
    params[@"game_id"] = [QPJHFunDataFormat QPJHGetGameID];
    params[@"role_id"] = roleInfo.super_role_id;
    params[@"server_id"] = roleInfo.service_id;
    params[@"player_level"] = roleInfo.role_level;
    params[@"random"] = random;
    params[@"sign"] = sign;
    
    ///boot/online
    [SQPJHNetworkManager getWithURL:AppendUrl(@"/report/online") Params:params success:^(id responseObject) {
        NSString *code = [NSString stringWithFormat:@"%@", responseObject[@"code"]];
        NSLog(@"/report/online responseObject=%@", responseObject);
        if ([code isEqualToString:SuccessCode]) {
            
//            NSArray *data = [responseObject bgl_safeMutableArrayAllKey:@"data"];
            
        }
        
    } failure:^(NSString *error) {
    }];
}

@end
